﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class OnLineLearnMore : DLL.MyWebPage
{
	private DLL.Training.Component clsTrainingComp;

	protected void Page_Load(object sender, EventArgs e)
	{
		if (!IsPostBack)
		{
			Show();
		}
	}

	protected override void OnInit(EventArgs e)
	{
		clsTrainingComp = new DLL.Training.Component(Page);

		base.OnInit(e);

		Grv.RowCommand += new GridViewCommandEventHandler(Grv_RowCommand);
		Grv.DataBound += new EventHandler(Grv_DataBound);
		LBtnFirst.Click += new EventHandler(LBtnFirst_Click);
		LBtnForward.Click += new EventHandler(LBtnForward_Click);
		LBtnNext.Click += new EventHandler(LBtnNext_Click);
		LBtnLast.Click += new EventHandler(LBtnLast_Click);
	}

	void Grv_DataBound(object sender, EventArgs e)
	{
		GridView gv = new GridView();

		gv = (GridView)sender;

		if (gv.PageIndex == 0)
		{
			LBtnFirst.Enabled = false;
			LBtnForward.Enabled = false;
		}
		else
		{
			LBtnFirst.Enabled = true;
			LBtnForward.Enabled = true;
		}
		if (gv.PageIndex == gv.PageCount - 1)
		{
			LBtnLast.Enabled = false;
			LBtnNext.Enabled = false;
		}
		else
		{
			LBtnLast.Enabled = true;
			LBtnNext.Enabled = true;
		}
	}

	/// <summary>
	/// 最后一页
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	void LBtnLast_Click(object sender, EventArgs e)
	{
		Grv.PageIndex = Grv.PageCount - 1;
		Show();
	}

	/// <summary>
	/// 下一页
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	void LBtnNext_Click(object sender, EventArgs e)
	{
		Grv.PageIndex = Grv.PageIndex + 1;
		Show();
	}

	/// <summary>
	/// 上一页
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	void LBtnForward_Click(object sender, EventArgs e)
	{
		Grv.PageIndex = Grv.PageIndex - 1;
		Show();
	}

	/// <summary>
	/// 第一页
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	void LBtnFirst_Click(object sender, EventArgs e)
	{
		Grv.PageIndex = 0;
		Show();
	}

	void Grv_RowDataBound(object sender, GridViewRowEventArgs e)
	{
		DLL.SupplyDemand.Component clsSupplyDemandComp = new DLL.SupplyDemand.Component(Page);
		int nSupplyDemandID = 0;
		GridView gv = new GridView();

		if (e.Row.RowIndex >= 0)
		{
			HyperLink hLink = new HyperLink();
			DLL.SupplyDemand.Entity clsSupplyDemand = new DLL.SupplyDemand.Entity();

			gv = (GridView)sender;
			nSupplyDemandID = Convert.ToInt32(gv.DataKeys[e.Row.RowIndex].Value);
			clsSupplyDemand = clsSupplyDemandComp.GetOne(nSupplyDemandID);
			hLink = (HyperLink)e.Row.FindControl("HLinkSupplyDemand");
			hLink.Target = "_blank";
			hLink.Text = "[" + clsSupplyDemand.TypeName + "]" + clsSupplyDemand.Name;
			hLink.NavigateUrl = "~/ShowFileDetail.aspx?FileContentID=" + clsSupplyDemand.FileContentID.ToString();
		}
	}

	void Grv_RowCommand(object sender, GridViewCommandEventArgs e)
	{
		int nUserID = 0;
		int nTrainingID = 0;
		DLL.TrainingPerson.Component clsTrainingPersonComp = new DLL.TrainingPerson.Component(Page);

		if (e.CommandName == "Learn")
		{
			if (Session["UserID"] != null)
			{
				if (Session["UserID"].ToString() != "")
				{
					nUserID = Convert.ToInt32(Session["UserID"]);
					nTrainingID = Convert.ToInt32(e.CommandArgument.ToString());
					if (clsTrainingPersonComp.Check(nTrainingID, nUserID))
					{
						Response.Redirect("OnLineLearn.aspx?TrainingID=" + nTrainingID.ToString() + "");
					}
					else
					{
						TonyNet4V1.ShowMessageAlert.ShowMessage(Page, "对不起，您不在此培训班中！");
					}
				}
				else
				{
					TonyNet4V1.ShowMessageAlert.ShowMessage(Page, "请登录！");
				}
			}
			else
			{
				TonyNet4V1.ShowMessageAlert.ShowMessage(Page, "请登录！");
			}
		}
	}

	private void Show()
	{
		Grv.DataSource = clsTrainingComp.QueryCommencementTraining();
		Grv.DataKeyNames = new string[] { "ID" };
		Grv.DataBind();
	}
}